package com.whimscrew.area.service;

import com.whimscrew.area.model.StoreChange;
import com.whimscrew.area.stream.CacheChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.data.redis.core.RedisTemplate;

@EnableBinding(CacheChannel.class)
public class CacheService {


    private final static Logger logger = LoggerFactory.getLogger(CacheService.class);

    @Autowired
    RedisTemplate redisTemplate;

    @StreamListener(CacheChannel.CACHE_CHANGE_CHANNEL)
    public void receiveMsg(StoreChange storeChange ) {
        logger.debug("接收缓存变更信息操作 ： "+ storeChange.getAction() + "  ,消息ID-"+storeChange.getTokenId());
        //可在这里进行请求到StoreService进行增删改Redsi缓存，或在这里增删改缓存处理
        //这里只对增加进行操作缓存
        redisTemplate.opsForHash().put("areaStore", storeChange.getAreaCode(), storeChange.getStoreList());
    }

}
